package pers.qianyu.module.comment.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import pers.qianyu.module.comment.service.CommentService;
import pers.qianyu.module.core.comm.HttpResult;
import pers.qianyu.module.core.comm.Pagination;
import pers.qianyu.module.core.domain.comment.dto.CommentDTO;
import pers.qianyu.module.core.domain.comment.query.CommentQuery;
import pers.qianyu.module.core.domain.comment.vo.CommentVO;
import pers.qianyu.module.logging.annotation.ControllerLog;
import pers.qianyu.module.logging.enums.BusinessType;

import java.util.List;

@RestController
@RequestMapping("comment-serv")
@Api(tags = "与评论相关的api接口")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @ControllerLog(businessType = BusinessType.QUERY)
    @GetMapping("/comment/{id}")
    @ApiOperation("根据评论的 id 查询评论列表")
    public HttpResult<CommentVO> getCommentById(@PathVariable(value = "id") Long id) {
        CommentVO commentVO = commentService.findById(id);
        return HttpResult.okWithData(commentVO);
    }

    // findCommentsByTopicId 可以支持多级评论
    //@GetMapping("/comments/{id}")
    //@ApiOperation("根据评论对象 id 查询评论信息")
    public HttpResult<List<CommentVO>> getCommentByTopicId(@PathVariable(value = "id") Long topicId) {
        List<CommentVO> comments = commentService.findCommentsByTopicId(topicId);
        return HttpResult.okWithData(comments);
    }

    @ControllerLog(businessType = BusinessType.QUERY)
    @GetMapping("/comments")
    @ApiOperation("分页查询评论信息")
    public HttpResult<Pagination<CommentVO>> query(CommentQuery commentQuery) {
        Pagination<CommentVO> pagination = commentService.query(commentQuery);
        return HttpResult.okWithData(pagination);
    }

    @ControllerLog(businessType = BusinessType.ADD)
    @PostMapping("/comment")
    @ApiOperation("新增评论")
    public HttpResult<?> insertComment(@Validated @RequestBody CommentDTO commentDTO) {
        commentService.insert(commentDTO);
        return HttpResult.okWithMessage("评论插入成功");
    }

    @ControllerLog(businessType = BusinessType.DELETE)
    @DeleteMapping("/comment/{id}")
    @ApiOperation("根据 id 删除评论")
    public HttpResult<?> delCommentById(@PathVariable Long id) {
        commentService.deleteById(id);
        return HttpResult.okWithMessage("删除评论成功");
    }
}
